package DataBase;

import java.sql.PreparedStatement;
import java.sql.SQLException;


import com.mysql.jdbc.ResultSet;

public class CheckUsername {
	
	private String username;
	private SQLConnection sc;
	
	
	public CheckUsername(String usern)
	{
		this.username=usern;
	}
	
	public int check()
	{
		int result=0;
		
		try
		{		
		
		sc=new SQLConnection();
		PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT username FROM Login WHERE username=? ;");		
		ps1.setString(1, username);
		ResultSet rsUN=(ResultSet) ps1.executeQuery();	
		
		if (rsUN.next()) result=1; // username is already exists		
		else result=0; // username does't exist	
	
		rsUN.close();
		ps1.close();
		 sc.getConnection().close();
		}
	 catch (SQLException ex) 
 	{/* handle any errors*/
	System.out.println("SQLException: " + ex.getMessage());
	System.out.println("SQLState: " + ex.getSQLState());
	System.out.println("VendorError: " + ex.getErrorCode());	
    }
		
		return result;
	}

}
